/**
 * GeoMatcher.as
 *
 * By Laurens Van Keer & Bram Truyaert
 */
 
import com.google.maps.InfoWindowOptions;
import com.google.maps.LatLng;
import com.google.maps.overlays.Marker;
import com.google.maps.overlays.MarkerOptions;
import com.google.maps.styles.FillStyle;
import com.google.maps.styles.StrokeStyle;

import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;

import mx.collections.ArrayCollection;

import scripts.GeoMatchMarker;

private var geocodes:XML;
private var markers:ArrayCollection;

private function initGeocodes():void
{
	var loader:URLLoader = new URLLoader();
	var request:URLRequest = new URLRequest("geocodes.xml");
	loader.load(request);
	loader.addEventListener(Event.COMPLETE, onCompleteGeocodes);
}

private function onCompleteGeocodes(event:Event):void
{
    var loader:URLLoader = event.target as URLLoader;
    if (loader != null) {
        geocodes = new XML(loader.data);
        trace(geocodes.toXMLString());
    } else {
        trace("loader is not a URLLoader!");
    }
}

private function handleGeoMatcher(event:ResultEvent):void
{
	var xmlResult:XML = new XML(event.result);
	markers = new ArrayCollection();
	for each (var match:XML in xmlResult.GeoMatch) {
		addCountryToMap(match.Country, match.Similarity);
 	}
}

private function getCountryColor(similarity:int):Number
{
	var countryColors:Array = new Array(0x0000ff, 0xff9600, 0xffad19, 0xffc331, 0xc6d944, 
		0x9ed77f, 0x93af45, 0x478735, 0x2b752b, 0x1f6e27);
	return countryColors[Math.floor(similarity/10)%10];
}

private function addCountryToMap(country:String, similarity:int):void
{
	var lat:Number = parseFloat(geocodes.Geocode.(Country == country).Lat[0].text());
	var lng:Number = parseFloat(geocodes.Geocode.(Country == country).Lng[0].text());
	
	if ( similarity >= 35 ) {
    	var countryMarker:GeoMatchMarker = new GeoMatchMarker(
    		new LatLng(lat,lng), 
    		new MarkerOptions({
    			strokeStyle: new StrokeStyle({color: 0x3e3e3e}),
    			fillStyle: new FillStyle({color: getCountryColor(similarity), alpha: 0.8}),
    			radius: 7,
    			hasShadow: true
    		})
    	);
    	countryMarker.country = country; countryMarker.similarity = similarity;
    	markers.addItem(countryMarker);
    	countryMarker.addEventListener(MapMouseEvent.CLICK, markerClicked);
		geoMatcherMap.addOverlay(countryMarker);     
	}
}

private function markerClicked(event:MapMouseEvent):void
{
    var marker:Marker = event.target as Marker;
    
    var info:String = "No info available! :(";
    for(var i:int=0; i<markers.length; i++) {
    	if(marker.getLatLng().toString() == markers[i].getLatLng().toString()) {
    		info = markers[i].country + "\n\nSimilarity: " + markers[i].similarity + "%";
    		break;
    	}
    }
    
    marker.openInfoWindow(new InfoWindowOptions({content:info}));
}

private function loadGeoMatcher():void
{
	geoMatcherMap.clearOverlays();
	geoMatcherXml.send();
}

private function onGeoMatcherMapReady(event:Event):void
{
	geoMatcherMap.enableScrollWheelZoom();
	geoMatcherMap.enableContinuousZoom();
	geoMatcherMap.addControl(new ZoomControl());
	geoMatcherMap.clearOverlays();
}